﻿using System;
using System.Drawing.Drawing2D;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace Paint
{
    public class DrawArrowStrategy : IDrawStrategy
    {
        private bool _canDraw = false;
        private Point _previous;
        private Polygon _poly = null;

        private BrushSettings _brushSettings;

        public DrawArrowStrategy(BrushSettings settings)
        {
            _brushSettings = settings;
        }

        public void OnMouseDown(Canvas canvas, Point point)
        {
            _previous = point;
            _canDraw = true;
        }

        public void OnMouseMove(Canvas canvas, Point point)
        {
            if (_canDraw == false)
                return;

            Point point1, point2, point3, point4;
            Point midpoint = new Point(0,0);
            PointCollection polygonPoints = null;

            if (_poly is null)
            {
                _poly = new Polygon();
                _poly.StrokeThickness = _brushSettings.StrokeThickness;
                _poly.Stroke = _brushSettings.Color;
                _poly.Points = polygonPoints;
                _poly.Stretch = Stretch.Uniform;
                _poly.Fill= _brushSettings.Color;

                canvas.Children.Add(_poly);
            }

            point1 = new Point(_previous.X, _previous.Y);
            point2 = new Point(point.X , point.Y);
            double k= (point.Y - _previous.Y) / (point.X - _previous.X);

            //箭头大小系数
            double k1;
            double k2;
            if (_brushSettings.StrokeThickness < 5)
            {
                k1 = 10;
                k2 = 3;
            }
            else
            {
                k1 = 10/3;
                k2 = 3/3;
            }
           
            double lx_mid = Math.Sqrt(Math.Pow(k1 * _brushSettings.StrokeThickness, 2) / (Math.Pow(k, 2) + 1));
            double ly_mid = Math.Abs(k * lx_mid);

            if (point.X >= _previous.X && point.Y >= _previous.Y)//第一象限
            {
                midpoint.X = point2.X - lx_mid;
                midpoint.Y = point2.Y - ly_mid;
            }
            else if (point.X <= _previous.X && point.Y >= _previous.Y)//第二象限
            {
                midpoint.X = point2.X + lx_mid;
                midpoint.Y = point2.Y - ly_mid;
            }
            else if (point.X <= _previous.X && point.Y <= _previous.Y)//第三象限
            {
                midpoint.X = point2.X + lx_mid;
                midpoint.Y = point2.Y + ly_mid;
            }
            else if (point.X >= _previous.X && point.Y <= _previous.Y)//第四象限
            {
                midpoint.X = point2.X - lx_mid;
                midpoint.Y = point2.Y + ly_mid;
            }

            double b = midpoint.Y - 1 / k * midpoint.X;

            point3 = new Point(0, 0);
            point4 = new Point(0, 0);
            double lx_3 = Math.Sqrt(Math.Pow(k2 * _brushSettings.StrokeThickness, 2) / (1 / Math.Pow(k, 2) + 1));
            double ly_3 = Math.Abs(-lx_3 / k);

            if (point.X >= _previous.X && point.Y >= _previous.Y)//第一象限
            {
                point3.X = midpoint.X - lx_3;
                point3.Y = midpoint.Y + ly_3;
            }
            else if (point.X <= _previous.X && point.Y >= _previous.Y)//第二象限
            {
                point3.X = midpoint.X - lx_3;
                point3.Y = midpoint.Y - ly_3;
            }
            else if (point.X <= _previous.X && point.Y <= _previous.Y)//第三象限
            {
                point3.X = midpoint.X - lx_3;
                point3.Y = midpoint.Y + ly_3;
            }
            else if (point.X >= _previous.X && point.Y <= _previous.Y)//第四象限
            {
                point3.X = midpoint.X - lx_3;
                point3.Y = midpoint.Y - ly_3;
            }
            
            double lx_4 = Math.Sqrt(Math.Pow(k2 * _brushSettings.StrokeThickness, 2) / (1 / Math.Pow(k, 2) + 1));
            double ly_4 = Math.Abs(-lx_4 / k);

            if (point.X >= _previous.X && point.Y >= _previous.Y)//第一象限
            {
                point4.X = midpoint.X + lx_4;
                point4.Y = midpoint.Y - ly_4;
            }
            else if (point.X <= _previous.X && point.Y >= _previous.Y)//第二象限
            {
                point4.X = midpoint.X + lx_4;
                point4.Y = midpoint.Y + ly_4;
            }
            else if (point.X <= _previous.X && point.Y <= _previous.Y)//第三象限
            {
                point4.X = midpoint.X + lx_4;
                point4.Y = midpoint.Y - ly_4;
            }
            else if (point.X >= _previous.X && point.Y <= _previous.Y)//第四象限
            {
                point4.X = midpoint.X + lx_4;
                point4.Y = midpoint.Y + ly_4;
            }
            
            //画多边形
            polygonPoints = new PointCollection();
            polygonPoints.Add(point1);
            polygonPoints.Add(midpoint);
            polygonPoints.Add(point3);
            polygonPoints.Add(point2);
            polygonPoints.Add(point4);
            polygonPoints.Add(midpoint);
            polygonPoints.Add(point1);
            _poly.Points = polygonPoints;

            //显示位置
            if (point.X >= _previous.X && point.Y >= _previous.Y)//第一象限
            {
                Canvas.SetLeft(_poly, _previous.X);
                Canvas.SetTop(_poly, _previous.Y);
            }
            else if (point.X <= _previous.X && point.Y >= _previous.Y)//第二象限
            {
                Canvas.SetLeft(_poly, point.X);
                Canvas.SetTop(_poly, _previous.Y);
            }
            else if (point.X <= _previous.X && point.Y <= _previous.Y)//第三象限
            {
                Canvas.SetLeft(_poly, point.X);
                Canvas.SetTop(_poly, point.Y);
            }
            else if (point.X >= _previous.X && point.Y <= _previous.Y)//第四象限
            {
                Canvas.SetLeft(_poly, _previous.X);
                Canvas.SetTop(_poly, point.Y);
            }
        }

        public void OnMouseUp(Canvas canvas, Point point)
        {
            _canDraw = false;
            _poly = null;
            _previous = point;
        }
    }
}
